//
//  BIDCommonRepairDetailViewController.m
//  HJGJCommunity
//
//  Created by mal on 2017/6/5.
//  Copyright © 2017年 mal. All rights reserved.
//

#import "BIDCommonRepairDetailViewController.h"
#import "BIDBrowserImageViewController.h"
#import "BIDShowPhotoView.h"

static NSString *repairDetailURL = @"client/repair/publicRepairInfo.action";

@interface BIDCommonRepairDetailViewController ()<BIDShowPhotoViewDelegate>

@end

@implementation BIDCommonRepairDetailViewController
@synthesize repairID;

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"报修详情";
    // Do any additional setup after loading the view from its     
    [self loadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)loadData
{
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getPayServerAddr], repairDetailURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"id=%@", repairID];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary *responseDic, NSString *errMsg){
        if(errMsg)
        {
            [self showAlertView:errMsg buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
        }
        else
        {
            NSString *strApplyTime = responseDic[@"createTime"];
            _timeLabel.text = strApplyTime;
            NSString *strStatus = @"";
            NSNumber *statusValue = responseDic[@"status"];
            if([statusValue intValue]==0)
            {
                strStatus = @"待处理";
                _statusLabel.textColor = RGBACOLOR(255.f, 142.f, 8.f, 1.f);
            }
            else
            {
                strStatus = @"已完成";
                _statusLabel.textColor = [UIColor darkGrayColor];
            }
            NSString *strContent = responseDic[@"content"];
            CGFloat limitWidth = screenSize.width-CGRectGetMaxX(_flagForContentLabel.frame)-8*2;
            CGFloat contentHeight = [BIDGlobalMethods getHeightWithString:strContent font:_contentLabel.font constraintSize:CGSizeMake(limitWidth, MAXFLOAT)];
            if(contentHeight > 21)
            {
                _heightForContentLabelConstraint.constant = contentHeight;
                [self.view layoutIfNeeded];
            }
            _contentLabel.text = strContent;
            //
            _showPhotoView.delegate = self;
            //
            NSMutableArray *imgPathArr = [NSMutableArray array];
            for(NSDictionary *dic in responseDic[@"repairFiles"])
            {
                NSString *strImgPath = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getPayServerAddr], dic[@"path"]];
                [imgPathArr addObject:strImgPath];
            }
            [_showPhotoView adjustLayoutWithImgPathArr:imgPathArr];
            _heightForShowPhotoViewConstraint.constant = CGRectGetHeight(_showPhotoView.frame);
            _heightForContainerViewConstraint.constant = CGRectGetMaxY(_showPhotoView.frame)+8;
            [self.view layoutIfNeeded];
            CGSize contentSize = _scrollView.contentSize;
            contentSize.height = CGRectGetMaxY(_showPhotoView.frame)+8;
            [_scrollView setContentSize:contentSize];
        }
    }];
}


#pragma mark - BIDShowPhotoViewDelegate
- (void)showPhotoAtIndex:(NSInteger)index withImgArr:(NSArray *)imgArr
{
    BIDBrowserImageViewController *vc = [[BIDBrowserImageViewController alloc] initWithNibName:@"BIDBrowserImageViewController" bundle:nil];
    vc.index = index;
    vc.imgArr = [[NSArray alloc] initWithArray:imgArr];
    [self.navigationController pushViewController:vc animated:YES];
}
@end
